#! /usr/bin/python
# encoding=utf-8
# Created by Fenglu Niu on 2025/5/26 11:13
from datetime import datetime
from decimal import Decimal
from typing import Callable, Self, Any

from pydantic import model_serializer
from sqlmodel import SQLModel, Field, Column
from sqlalchemy import Integer, String, DateTime, DECIMAL, Text

name = 'entity'


# 序列化


class Entity(SQLModel):
    id: int = Field(default=None, primary_key=True, sa_type=Integer, sa_column_kwargs={'comment': '主键ID'})

    @model_serializer(mode="wrap")
    def serialize(
            self, original_serializer: Callable[[Self], dict[Any, Any]]
    ) -> dict[Any, Any]:
        for field_name, field_info in self.model_fields.items():
            if field_info.annotation == datetime:
                field_value = getattr(self, field_name)
                if not field_value:
                    continue
                setattr(
                    self,
                    field_name,
                    field_value.strftime("%Y-%m-%d %H:%M:%S"),
                )
        result = original_serializer(self)
        return result


class Case(Entity, table=True):
    __tablename__ = 't_case'
    __table_args__ = {'comment': '案件表'}
    case_task_id: int = Field(default=None, sa_column=Column(Integer, comment='案件任务ID'))
    input_unit_id: int = Field(None, sa_column=Column(Integer, comment='登记机构ID'))
    input_unit_name: str = Field(None, sa_column=Column(String(length=64), comment='登记机构名称'))
    area_name: str = Field(None, sa_column=Column(String(length=64), comment='问题属地'))
    canal_name: str = Field(None, sa_column=Column(String(length=20), comment='事项来源名称'))
    case_level: str = Field(None, sa_column=Column(String(length=4), comment='事项等级'))
    case_ref: str = Field(None, sa_column=Column(String(length=128), comment='事项编号'))
    case_type: str = Field(None, sa_column=Column(String(length=16), comment='事项类型'))
    case_type_name: str = Field(None, sa_column=Column(String(length=64), comment='事项类型名称'))
    mediate_unit_name: str = Field(None, sa_column=Column(String(length=64), comment='承办部门名称'))
    mediate_unit_id: int = Field(None, sa_column=Column(Integer, comment='承办部门ID'))
    assist_unit_id: int = Field(None, sa_column=Column(Integer, comment='协助部门ID'))
    assist_unit_name: str = Field(None, sa_column=Column(String(length=64), comment='协助部门名称'))
    status: int = Field(None, sa_column=Column(Integer, comment='状态'))
    status_name: str = Field(None, sa_column=Column(String(length=16), comment='状态名称'))
    medi_result_name: str = Field(None, sa_column=Column(String(length=16), comment='化解结果名称'))
    plaintiffs: str = Field(None, sa_column=Column(String(length=128), comment='申请方'))
    defendants: str = Field(None, sa_column=Column(String(length=128), comment='被申请方'))
    occur_time: datetime = Field(None, sa_column=Column(DateTime, comment='发生时间'))
    major_status: int = Field(None, sa_column=Column(Integer, comment='是否重大矛盾纠纷'))
    is_risk: int = Field(None, sa_column=Column(Integer, comment='是否扬言风险纠纷'))
    case_claim: str = Field(None, sa_column=Column(Text, comment='事项申请'))
    case_des: str = Field(None, sa_column=Column(Text, comment='事项概况'))
    addr: str = Field(None, sa_column=Column(Text, comment='纠纷发生地点'))
    amount: Decimal = Field(None, sa_column=Column(DECIMAL(15, 2), comment='金额'))
    people_num: int = Field(None, sa_column=Column(Integer, comment='人数'))
    visit_way_name: str = Field(None, sa_column=Column(String(length=64), comment='来访形式'))
    visit_people_num: int = Field(None, sa_column=Column(Integer, comment='来访人数'))
    visit_time: datetime = Field(None, sa_column=Column(DateTime, comment='来访时间'))
    create_time: datetime = Field(None, sa_column=Column(DateTime, comment='登记时间'))
    close_time: datetime = Field(None, sa_column=Column(DateTime, comment='办结时间'))


class CaseQuery(SQLModel):
    input_unit_name: str = Field(None)
    area_name: str = Field(None)
    reg_time_start: datetime = Field(None)
    reg_time_end: datetime = Field(None)
    close_time_start: datetime = Field(None)
    close_time_end: datetime = Field(None)
    case_type_name: str = Field(None)
    canal_name: str = Field(None)
    case_level: str = Field(None)
    medi_result_name: str = Field(None)
    status_name: str = Field(None)
    mediate_unit_name: str = Field(None)
    assist_unit_name: str = Field(None)
    case_ref: str = Field(None)
    plaintiffs: str = Field(None)
    defendants: str = Field(None)
    major_status: int = Field(None)
    is_risk: int = Field(None)
    people_num: int = Field(None)
    amount: int = Field(None)



class Person(Entity, table=True):
    __tablename__ = 't_person'
    __table_args__ = {'comment': '人员表'}
    case_id: int = Field(None, sa_column=Column(Integer, comment='案件ID'))
    per_type: str = Field(None, sa_column=Column(String(length=16), comment='人员类型'))
    per_type_name: str = Field(None, sa_column=Column(String(length=16), comment='人员类型名称'))
    per_class_name: str = Field(None, sa_column=Column(String(length=16), comment='人员类别'))
    true_name: str = Field(None, sa_column=Column(String(length=20), comment='真实姓名'))
    mobile: str = Field(None, sa_column=Column(String(length=20), comment='联系方式'))
    sex_name: str = Field(None, sa_column=Column(String(length=4), comment='性别名称'))
    nation_name: str = Field(None, sa_column=Column(String(length=12), comment='民族名称'))
    certi_type_name: str = Field(None, sa_column=Column(String(length=16), comment='证件类型名称'))
    certi_no: str = Field(None, sa_column=Column(String(length=20), comment='证件号码'))
    addr: str = Field(None, sa_column=Column(String(length=128), comment='地址'))
    place_addr: str = Field(None, sa_column=Column(String(length=128), comment='户籍地址'))
    age: int = Field(None, sa_column=Column(Integer, comment='年龄'))
    work_unit: str = Field(None, sa_column=Column(String(length=64), comment='工作单位'))
    extreme: int = Field(None, sa_column=Column(Integer, comment='是否极端'))


class CaseResult(Entity, table=True):
    __tablename__ = 't_case_result'
    __table_args__ = {'comment': '案件结果表'}
    case_id: int = Field(None, sa_column=Column(Integer, comment='案件ID'))
    medi_result_name: str = Field(None, sa_column=Column(String(length=10), comment='化解结果'))
    mediator: str = Field(None, sa_column=Column(String(length=10), comment='经办人'))
    mediate_unit_name: str = Field(None, sa_column=Column(String(length=16), comment='承办部门名称'))
    assist_unit_name: str = Field(None, sa_column=Column(String(length=16), comment='配合部门名称'))
    accept_time: datetime = Field(None, sa_column=Column(DateTime, comment='受理时间'))
    close_time: datetime = Field(None, sa_column=Column(DateTime, comment='办结时间'))
    handle_content: str = Field(None, sa_column=Column(String(length=255), comment='办理意见'))
    windup_content: str = Field(None, sa_column=Column(String(length=255), comment='协议履行情况'))
    agree_type_name: str = Field(None, sa_column=Column(String(length=16), comment='协议类型名称'))
    agree_files: str = Field(None, sa_column=Column(String(length=2048), comment='协议文件'))
    agree_content: str = Field(None, sa_column=Column(String(length=1000), comment='协议要点'))


class CaseFeedback(Entity, table=True):
    __tablename__ = 't_case_feedback'
    __table_args__ = {'comment': '案件反馈表'}
    case_id: int = Field(None, sa_column=Column(Integer, comment='案件ID'))
    handle_unit_name: str = Field(None, sa_column=Column(String(length=16), comment='承办部门名称'))
    handle_user_name: str = Field(None, sa_column=Column(String(length=16), comment='操作人'))
    handle_content: str = Field(None, sa_column=Column(String(length=255), comment='办理意见'))
    handle_files: str = Field(None, sa_column=Column(String(length=2048), comment='办理附件'))
    create_time: datetime = Field(None, sa_column=Column(DateTime, comment='创建时间'))


class CaseFlow(SQLModel, table=True):
    __tablename__ = 't_case_flow'
    __table_args__ = {'comment': '案件流程表'}
    id: int = Field(default=None, sa_column=Column(Integer, primary_key=True, comment='主键ID', autoincrement=True))
    case_id: int = Field(None, sa_column=Column(Integer, comment='案件ID'))
    node_show_name: str = Field(None, sa_column=Column(String(length=16), comment='节点名称'))
    handle_unit_name: str = Field(None, sa_column=Column(String(length=16), comment='承办部门名称'))
    handle_user_name: str = Field(None, sa_column=Column(String(length=16), comment='操作人'))
    handle_time: datetime = Field(None, sa_column=Column(DateTime, comment='操作时间'))
    handle_notes: str = Field(None, sa_column=Column(String(length=255), comment='操作备注'))
    medi_result_name: str = Field(None, sa_column=Column(String(length=16), comment='化解结果'))
    audit_result_name: str = Field(None, sa_column=Column(String(length=16), comment='审核结果'))


class CaseApplyRecord(Entity, table=True):
    __tablename__ = 't_case_apply_record'
    __table_args__ = {'comment': '案件申请记录表'}
    case_id: int = Field(None, sa_column=Column(Integer, comment='案件ID'))
    type: str = Field(None, sa_column=Column(String(length=16), comment='类型名称'))
    audit_result_name: str = Field(None, sa_column=Column(String(length=16), comment='审核结果'))
    medi_result_name: str = Field(None, sa_column=Column(String(length=16), comment='化解结果'))
    windup_content: str = Field(None, sa_column=Column(String(length=255), comment='协议履行情况'))
    apply_time: datetime = Field(None, sa_column=Column(DateTime, comment='申请时间'))
    apply_unit_name: str = Field(None, sa_column=Column(String(length=16), comment='申请部门名称'))
    apply_user_name: str = Field(None, sa_column=Column(String(length=16), comment='申请人'))
    audit_content: str = Field(None, sa_column=Column(String(length=255), comment='审核意见'))
    audit_time: datetime = Field(None, sa_column=Column(DateTime, comment='审核时间'))
    audit_unit_name: str = Field(None, sa_column=Column(String(length=16), comment='审核部门名称'))
    audit_user_name: str = Field(None, sa_column=Column(String(length=16), comment='审核人'))
